﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ACH
{
    public partial class CreateTraining : Form
    {
        string _trainingID;
        bool _valuesChanged = false;

        public CreateTraining()
        {
            InitializeComponent();
        }

        private void CreateTraining_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'aCHDataSet.TrainingParticipants' table. You can move, or remove it, as needed.
            this.trainingParticipantsTableAdapter.Fill(this.aCHDataSet.TrainingParticipants);
            // TODO: This line of code loads data into the 'aCHDataSet.TrainingTypes' table. You can move, or remove it, as needed.
            this.trainingTypesTableAdapter.Fill(this.aCHDataSet.TrainingTypes);
            // TODO: This line of code loads data into the 'aCHDataSet.Locations' table. You can move, or remove it, as needed.
            this.locationsTableAdapter.Fill(this.aCHDataSet.Locations);

        }

        private void btnAddParticipants_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_trainingID))
            {
                _trainingID = CreateOneTraining();
                _valuesChanged = false;
            }
            CreateTrainingParticipant part = new CreateTrainingParticipant(_trainingID);
            part.ShowDialog();
            trainingParticipantsBindingSource.ResetBindings(true);

        }

        private void btnCreateTraining_Click(object sender, EventArgs e)
        {
            if (_valuesChanged)
            {
                if (string.IsNullOrEmpty(_trainingID))
                    CreateOneTraining();
                else
                    UpdateOneTraining();
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void UpdateOneTraining()
        {
            ACHDataSetTableAdapters.TrainingsTableAdapter log = new ACHDataSetTableAdapters.TrainingsTableAdapter();
            DataTable dt = log.GetData();
            DataRow r = dt.Rows.Find(_trainingID);

        }


        private string CreateOneTraining()
        {
            ACHDataSetTableAdapters.TrainingsTableAdapter log = new ACHDataSetTableAdapters.TrainingsTableAdapter();
            string guid = Guid.NewGuid().ToString();
            log.Insert(guid, txtTrainingName.Text, dateStart.Value, dateEnd.Value, (cmbTrainingLocation.SelectedValue as DataRowView)["ID"].ToString(),
                (cmbTrainingType.SelectedValue as DataRowView)["ID"].ToString(), DateTime.Now, DateTime.Now, "PAUL", "PAUL");
            return guid;
        }

        private void txtTrainingName_TextChanged(object sender, EventArgs e)
        {
            _valuesChanged = true;
        }

        private void dateStart_ValueChanged(object sender, EventArgs e)
        {
            _valuesChanged = true;
        }

        private void dateEnd_ValueChanged(object sender, EventArgs e)
        {
            _valuesChanged = true;
        }

        private void cmbTrainingLocation_SelectedValueChanged(object sender, EventArgs e)
        {
            _valuesChanged = true;
        }

        private void cmbTrainingType_SelectedIndexChanged(object sender, EventArgs e)
        {
            _valuesChanged = true;
        }

        private void dateEnd_Validating(object sender, CancelEventArgs e)
        {
            if (dateEnd.Value < dateStart.Value)
            {
                MessageBox.Show("End date < Start date");
                e.Cancel = true;
            }
        }

        private void dateStart_Validating(object sender, CancelEventArgs e)
        {
            if (dateEnd.Value < dateStart.Value)
            {
                MessageBox.Show("End date < Start date");
                e.Cancel = true;
            }
        }
    }
}
